<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <link rel="stylesheet" href="/static/font/iconfont.css">

</head>
<body>
<div style="margin:40px;" class="layui-form">
    <input id="touristAttraction" type="hidden" value="<$ touristAttraction $>">
    <div class="layui-form-item">
        <label class="layui-form-label">签到点名称</label>
        <div class="layui-input-block">
            <select name="interest" lay-filter="aihao" id="view">

            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">major</label>
        <div class="layui-input-block">
            <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入major"
                   id="major" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">minor</label>
        <div class="layui-input-block">
            <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入minor" id="minor"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" style="margin-top: 40px">
        <div class="layui-input-block">
            <button class="layui-btn" id="confirm" onclick="confirmData()">确定</button>
            <button type="reset" class="layui-btn layui-btn-primary" onclick="closeHtml()">取消</button>
        </div>
    </div>
</div>
</body>

<!--[if lt IE 9]>
<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<script src="/static/layui/layui.js"></script>
<script>
  var $, layer, postData;

  function confirmData() {
    var attraction = $('#view').val();
    var major = $('#major').val();
    var minor = $('#minor').val();
    if (!major) {
      layer.alert('请先输入major');
      return
    }
    if (!minor) {
      layer.alert('请先输入minor');
      return
    }
    var data = {
      attraction: attraction,
      major: major,
      minor: minor
    };
    if (postData) {
      data.id = postData.id;
      editBeacon(data)
    } else {
      addBeacon(data)
    }
  }

  function editBeacon(data) {
    $.get('/beacon/edit?x-tm-token=' + localStorage.tmToken, data, function (response) {
      if (response.errno == 0) {
        closeHtml();
      } else {
        layer.alert('添加失败 ' + response.errmsg);
      }
    });
  }

  function addBeacon(data) {
    $.get('/beacon/add?x-tm-token=' + localStorage.tmToken, data, function (response) {
      if (response.errno == 0) {
        closeHtml();
      } else {
        layer.alert('添加失败 ' + response.errmsg);
      }
    });
  }

  function closeHtml() {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
    parent.window.location.reload();
  }

  layui.use(['layer', 'form'], function () {
    $ = layui.$;
    layer = layui.layer;
    if (getUrlParam(location.href, 'id')) {
      postData = {
        id: getUrlParam(location.href, 'id'),
        major: getUrlParam(location.href, 'major'),
        minor: getUrlParam(location.href, 'minor'),
        attraction: getUrlParam(location.href, 'attraction'),
      };
    }


    var htmlStr = '';
    var arr = JSON.parse($("#touristAttraction").val());
    for (var i = 0; i < arr.length; i++) {
      if (postData && postData.attraction == arr[i].type_code) {
        htmlStr += '<option value="' + arr[i].type_code + '" selected="">' + arr[i].name + '</option>';
      } else {
        htmlStr += '<option value="' + arr[i].type_code + '">' + arr[i].name + '</option>';
      }
    }
    $('#view').html(htmlStr);
    var form = layui.form;
    form.render();
    if (postData) {
      $('#major').val(postData.major);
      $('#minor').val(postData.minor);
    }
  });

  // 获取URL参数 e.g. map.html?sid=122134
  function getUrlParam(s, name) {
    var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
    if (reg.test(s))
      return unescape(RegExp.$2.replace(/\+/g, " "));
    return "";
  }

</script>
